//
//  MapViewController.m
//  GOGOTravel
//
//  Created by dllo on 16/3/4.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "MapViewController.h"
#import "Days.h"
#import <MapKit/MapKit.h>
@interface MapViewController ()<AMapSearchDelegate, MAMapViewDelegate>

@property (nonatomic, retain) MAMapView *mapView;
@property (nonatomic, retain) NSMutableArray *dataModel;
/**
 *  地理编码对象
 */
@property (nonatomic ,strong) CLGeocoder *geocoder;
@end

@implementation MapViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc{
    [_arr release];
    [_point release];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"当地位置";
    self.view.backgroundColor = UIColorFromRGB(0xF0F0F0);
    
  
    self.dataModel = [NSMutableArray array];
    
    
    if (_arr.count > 0) {
        for (Days *model in _arr) {
            if (model.location != nil) {
                [_dataModel addObject:model.location];
                
            }
        }
        
    }
    NSLog(@"%@", _arr);
    
    [self createMapView];
    
    
    
    
    
}

/** 创建地图 */
- (void)createMapView{
    [MAMapServices sharedServices].apiKey = APIKey;
    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width ,self.view.frame.size.height )];

    _mapView.delegate = self;
    _mapView.compassOrigin = CGPointMake(_mapView.compassOrigin.x, 22);
    _mapView.scaleOrigin = CGPointMake(_mapView.scaleOrigin.x, 22);
    //打开定位功能
    _mapView.showsUserLocation = YES;
    [self.view addSubview: _mapView];

//    MAAnnotationView *annotation = [[MAAnnotationView alloc] init];
    
    NSDictionary *dic = [_dataModel firstObject];
   

    
//    NSInteger latitude =  [dic[@"latitude"] integerValue];
//    NSInteger longitude = [dic[@"longitude"] integerValue];
//    
//     [_mapView setCenterCoordinate:CLLocationCoordinate2DMake(latitude,longitude) animated:YES];
//    
//    _mapView.showTraffic = YES;
//    
//    MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
//    annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
//    annotation.title = @"我的位置";
//    
//    //   ...............
//    [_mapView addAnnotation:annotation];
    
    
    
    
  [ UIView animateWithDuration:0.3 * (_dataModel.count) animations:^{
      for (NSDictionary *dic in _dataModel) {
          NSInteger latitude =  [dic[@"latitude"] integerValue];
          NSInteger longitude = [dic[@"longitude"] integerValue];
          
          [_mapView setCenterCoordinate:CLLocationCoordinate2DMake(latitude,longitude) animated:YES];
          _mapView.shouldGroupAccessibilityChildren = YES;
          
          MAPointAnnotation *annotation = [[MAPointAnnotation alloc] init];
          annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude);
    

          [_mapView addAnnotation:annotation];
          
          
          
      }
      
  }];
    
//    
//          CLLocationCoordinate2D location = CLLocationCoordinate2DMake(latitude, longitude);
//
//          MAPointAnnotation *point = [[MAPointAnnotation alloc] init];
//
//
//
//          [point setCoordinate:location];
//           point.title = @"我的位置";
//
//          [_mapView addAnnotation:point];
//
//          MACoordinateRegion region = _mapView.region;
//
//          region.center = location;
//          region.span.longitudeDelta /= 5.0;
//          region.span.latitudeDelta /= 5.0;
//          [_mapView setRegion:region animated:YES];

}

- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
    
    [self.geocoder reverseGeocodeLocation:userLocation.location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
       
        
        userLocation.title = @"weizhi";
        
    }];
    
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation{
    
    MAAnnotationView *annView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:nil];
    annView.canShowCallout = YES;
    annView.backgroundColor = [UIColor redColor];
    annView.draggable = YES;
    annView.annotation = annotation;
    return annView;
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
